<template>
  <div>
    <el-form @submit.native.prevent :inline="true" :model="params">
      <el-form-item>
        <c-select
          :options="enumBusType"
          number-key
          clearable
          placeholder="业务模式"
          v-model="busType"
          @change="getData()"
        ></c-select>
      </el-form-item>
      <el-form-item>
        <select-org-bank
          v-model="bankId"
          placeholder="选择银行"
          filterable
          clearable
          @select="selectBank"
        ></select-org-bank>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          placeholder="地址查询"
          auto-top
          clearable
          :bottom-level="2"
          @change="getData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <c-select
          :options="{ 0: '否', 1: '是' }"
          v-model="params['is_pro_gua.eq']"
          clearable
          placeholder="是否省直"
          @change="getData()"
        ></c-select>
      </el-form-item>
      <el-form-item>
        <c-select
          v-model="loanBehStatusSelected"
          placeholder="项目状态选择"
          clearable
          number-key
          :options="{ 1: '在保项目', 2: '解保项目' }"
          @change="handleStatusChange"
        ></c-select>
      </el-form-item>
      <el-form-item>
        <c-select
          :options="enumCompanyType"
          v-model="customerType"
          clearable
          multiple
          placeholder="客户类型"
          @change="customerTypeChange"
        ></c-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getData()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="onExportExcel('工信报表')" :loading="exportLoading">导出</el-button>
      </el-form-item>
    </el-form>
    <c-table
      border
      class="table-sm"
      v-loading="loading"
      :columns="tableColumns"
      :data="tableData"
    ></c-table>

    <div class="query-pagination">
      <div class="text-muted">共计{{ totalCount }}条/{{ fundsCount }}万元</div>
      <el-pagination
        layout="prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import SelectOrgBank from '@/bigcredit/views/program/components/SelectOrgBank'
import SelectAddress from '@/components/select/SelectAddress'
import { enumBusType } from './reportMixins'
import { exportExcel } from '@/credit/mixin/taskTableProvinceUnion'
import { mapGetters } from 'vuex'
export default {
  components: {
    SelectOrgBank,
    SelectAddress,
  },
  data() {
    return {
      enumCompanyType: {
        // 'org_enterprise_n_person': '有限责任公司(自然人独资)',
        org_enterprise: '农业企业',
        // 'org_enterprise_nh': '有限责任公司(自然人投资或控股)',
        // 'org_enterprise_sp': '有限责任公司(国有独资)',
        // 'org_enterprise_fb': '有限责任公司(外商投资)',
        // 'org_enterprise_fbo': '有限责任公司(外商独资）',
        // 'org_enterprise_cls': '股份有限公司',
        org_collective: '集体经济',
        org_cooperative: '农民专业合作社',
        org_farm: '家庭农场',
        org_large_breed: '种养殖大户',
        org_individual: '个体工商',
        org_farmer: '农户',
      },
      enumBusType,
      params: {
        index: 1,
        size: 15,
        'bank_id.eq': '',
        isBIQuery: 1,
        fields: [
          'address_name_pro',
          'address_name_city',
          'address_name_county',
          'customer_name',
          'corp_name',
          'corp_phone',
          'register_no',
          'lender_idcard',
          'main_business',
          'bank_name',
          'funds_use_dir',
          'warranty_amount',
          'load_end_date',
          'load_date',
          'org_class_name',
          'load_rate',
          'sale_income',
          'assets_funds',
          'warranty_rate',
          'warranty_fee',
          'other_cost',
          'ensurce_contract',
          'gua_contract',
          'id_card',
        ],
      },
      order: {
        load_date: 'desc',
      },
      busType: '',
      loading: false,
      tableName: 'market_project',
      totalCount: 0,
      tableData: [],
      tableColumns: [
        { attrs: { label: '归属省', prop: 'address_name_pro' } },
        { attrs: { label: '归属市', prop: 'address_name_city' } },
        { attrs: { label: '归属县', prop: 'address_name_county' } },
        { attrs: { label: '客户类型', prop: 'org_class_name' } },
        { attrs: { label: '受保企业名称或贷款人姓名', prop: 'customer_name' } },
        { attrs: { label: '受保企业联系人姓名', prop: 'corp_name' } },
        { attrs: { label: '受保企业联系人电话', prop: 'corp_phone' } },
        { attrs: { label: '三证合一', prop: 'paperwork' } },
        { attrs: { label: '统一社会信用代码', prop: 'register_no' } },
        { attrs: { label: '组织机构代码', prop: 'orgCode' } },
        { attrs: { label: '身份证号码', prop: 'id_card' } },
        { attrs: { label: '行业', prop: 'mainBusiness' } },
        { attrs: { label: '行业小类', prop: 'main_business' } },
        { attrs: { label: '销售收入（万元）', prop: 'sale_income' } },
        { attrs: { label: '资产总额（万元）', prop: 'assets_funds' } },
        { attrs: { label: '从业人数（人）', prop: 'e' } },
        { attrs: { label: '担保业务类型', prop: 'loadType' } },
        { attrs: { label: '放款银行名称', prop: 'bank_name' } },
        { attrs: { label: '贷款用途', prop: 'funds_use_dir' } },
        { attrs: { label: '担保（放款）金额（万元）', prop: 'warranty_amount' } },
        { attrs: { label: '担保机构与受保企业合同号', prop: 'gua_contract' } },
        { attrs: { label: '放款日期', prop: 'loadDate' } },
        { attrs: { label: '还款日期', prop: 'loadEndDate' } },
        { attrs: { label: '年化费率', prop: 'warranty_rate' } },
        { attrs: { label: '已收担保费', prop: 'warranty_fee' } },
        { attrs: { label: '收取其它费用（包括评审费等）', prop: 'other_cost' } },
        { attrs: { label: '收取受保企业保证金', prop: 'j' } },
        { attrs: { label: '贷款银行和受保企业合同号', prop: 'ensurce_contract' } },
        { attrs: { label: '银行贷款利率', prop: 'load_rate' } },
        { attrs: { label: '担保机构向金融机构存入保证金', prop: 'm' } },
        { attrs: { label: '备注', prop: 'remark' } },
        { attrs: { label: '公司放款流水号', prop: 'n' } },
        { attrs: { label: '银行确认放款金额', prop: 'bankFunds' } },
      ],
      exportLoading: false,
      loanBehStatusSelected: '',
      customerType: [],
      bankId: '',
      fundsCount: '',
    }
  },
  computed: {
    ...mapGetters(['clientId']),
  },
  created() {
    this.params['orWhere.service_id.eq'] = this.clientId
    this.params['orWhere.recommend_service.eq'] = this.clientId
    this.getData()
  },
  methods: {
    selectBank(bank) {
      this.params['bank_code.endlike'] = bank.agency_code
      this.getData()
    },
    customerTypeChange(value) {
      this.params['org_class.in'] = value.join(',')
      this.getData()
    },
    handleStatusChange(value) {
      this.params['load_end_date.gt'] = ''
      this.params['load_end_date.lte'] = ''
      if (!value) {
        return this.getData()
      }
      if (value === 1) {
        this.params['load_end_date.gt'] = this.$utils.dateFormat(new Date(), 'yyyy-MM-dd')
      } else if (value === 2) {
        this.params['load_end_date.lte'] = this.$utils.dateFormat(new Date(), 'yyyy-MM-dd')
      }
      this.getData()
    },
    getParams() {
      return this.params
    },
    fetchData(params) {
      return this.$utils.promise(this.$api.getData(this.$serviceName, this.tableName, params))
    },
    getData(startIndex = 1) {
      this.params.index = startIndex
      this.params['gua_type.in'] =
        this.busType === 1 ? '3' : this.busType === 0 ? '1,2,4' : '1,2,3,4'
      this.loading = true
      this.$api
        .getData(this.$serviceName, this.tableName, this.params)
        .then(res => {
          const data = (res && res.data) || {}
          this.tableData = ((data && data.data) || []).map(this.dataAdapter)
          this.totalCount = (data && data.totalCount) || 0
        })
        .finally(() => {
          this.loading = false
        })
      const { index, size, fields, ...params } = { ...this.params }
      params.groupFunc = 'round(sum(warranty_amount)|2)'
      this.$api.getData(this.$serviceName, this.tableName, params).then(res => {
        const data = (res && res.data && res.data.length && res.data[0]) || {}
        this.fundsCount = data.p
      })
    },
    dataAdapter(item) {
      item.paperwork = '否'
      item.orgCode = 0
      item.orgCode = item.register_no || ''
      item.bankFunds = item.warranty_amount
      item.mainBusiness = '农、林、牧、渔业'
      item.paperwork = item.register_no ? '是' : '否'
      item.loadType = '贷款担保'
      item.funds_use_dir = item.funds_use_dir || '流动资金贷款'
      item.loadDate = this.$utils.dateFormat(item.load_date, 'yyyy/MM/dd')
      item.loadEndDate = this.$utils.dateFormat(item.load_end_date, 'yyyy/MM/dd')
      return item
    },
    async onExportExcel(filename) {
      this.exportLoading = true
      exportExcel({
        filename,
        tableColumns: this.tableColumns,
        params: this.params,
        dataAdapter: this.dataAdapter,
        fetchData: this.fetchData,
        onLoaded: () => {
          this.exportLoading = false
        },
      })
    },
  },
}
</script>
<style lang="scss" scoped></style>
